//
//  MSSetViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/18.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSSetViewController.h"
#import "MSSetTableViewController.h"
#import "MSRegistViewController.h"
#import "MSCacheManager.h"

@interface MSSetViewController ()
@property (nonatomic, strong) MSSetTableViewController *tableVC;
@property (nonatomic, strong) GXButton              *quitBtn;

@end

@implementation MSSetViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    titleLable.text = @"设置";
}

- (void)initUI{
    self.tableVC = [[MSSetTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = self.contentView.bounds;
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
    
    UIView *footerView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
    footerView.backgroundColor = kDefaultViewBackgroundColor;
    self.tableVC.tableView.tableFooterView = footerView;
    
    self.quitBtn = [[GXButton alloc] initWithFrame:CGRectMake(12, 25, SCREEN_WIDTH-24, 45)];
    self.quitBtn.titleLabel.font = HeitiFont(16);
    [self.quitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [self.quitBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    [self.quitBtn setBackgroundColor:[UIColor whiteColor]];
    self.quitBtn.layer.borderColor = kBlueColor.CGColor;
    self.quitBtn.layer.borderWidth = 1;
    self.quitBtn.layer.cornerRadius = 2;
    [self.quitBtn addTarget:self action:@selector(quitClick) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:self.quitBtn];
}

- (void)testDataFunc{
    float cacheSize = [[MSCacheManager sharedCacheManager] cacheFileSize];
    NSString *sizeStr = [HDUtil notRounding:[NSString stringWithFormat:@"%F",cacheSize] afterPoint:2];
    NSString *unitStr = [NSString stringWithFormat:@"%@M",sizeStr];
    self.tableVC.dataArray = [NSMutableArray arrayWithArray:
                             @[[DOKeyValue key:@"修改密码" value:@""],
                               [DOKeyValue key:@"清除缓存" value:unitStr],
                               ]
                             ];
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"Logout"]!=nil){
            [HDUtil showMessage:@"退出成功"];
            NSUserDefaults *defaultes = [NSUserDefaults standardUserDefaults];
            [defaultes setObject:@"" forKey:LoginState];
            [defaultes setObject:@"" forKey:LoginSessionId];
            [defaultes synchronize];
            
            netEngine.sessionId = @"";
            [rootNavigation popToRootViewControllerAnimated:NO];
            rootDelegate.mainViewController.selectedTabBarIndex = 0;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        MSRegistViewController *vc = [[MSRegistViewController alloc] init];
        vc.registType = MSRegistType_ResetPassword;
        [rootNavigation pushViewController:vc animated:YES];
    }
    if (indexPath.row == 1) {
        [[MSCacheManager sharedCacheManager] clearCacheAction];
        
        [SXProgressHUD showHUDAddedTo:self.contentView animated:YES];
        
        [self performBlock:^{
            DOKeyValue *keyValue = self.tableVC.dataArray[1];
            keyValue.value = @"0M";
            [_tableVC.tableView reloadData];
            [SXProgressHUD hideAllHUDsForView:self.contentView animated:YES];
        } afterDelay:0.5];
    }
}

- (void)quitClick{
    [netEngine requestLogoutWithTarget:self.VctrID hudView:self.contentView];
//    NSUserDefaults *defaultes = [NSUserDefaults standardUserDefaults];
//    [defaultes setObject:@"" forKey:LoginState];
//    [defaultes setObject:@"" forKey:LoginSessionId];
//    [defaultes synchronize];
//    
//    netEngine.sessionId = @"";
//    [rootNavigation popToRootViewControllerAnimated:NO];
//    rootDelegate.mainViewController.selectedTabBarIndex = 0;
}

@end
